package com.jack.payment.util.common;

import com.jack.payment.constant.ResultHintConstant;
import com.jack.payment.entity.common.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class PageResultUtil {
    private Logger log = LoggerFactory.getLogger(PageResultUtil.class);

    /**
     * 受保护的方法, 计算返回时的页数
     * @param requestCount
     * @param resultCount
     * @return
     */
    public Integer resultPageComputer(Integer requestCount,
                                         Integer resultCount) {
        if (resultCount == 0) {
            resultCount = 0;
        } else if (resultCount % requestCount == 0) {
            resultCount = resultCount / requestCount;
        } else {
            resultCount = resultCount / requestCount + 1;
        }
        return resultCount;
    }

    /**
     * 判断请求条数 是否超过系统应有数据条数
     * @param requestPage
     * @param resultCount
     * @throws BusinessException
     */
    public void judgeRequestCount(Integer requestPage,
                                    Integer resultCount) throws BusinessException {
        if (requestPage >= resultCount) {
            log.warn("获取数据越界, 请求条数{}, 系统数据总条数{}", requestPage, resultCount);
            throw new BusinessException(ResultHintConstant.DATA_COUNT_EXCEED);
        }
    }
}
